package com.david.appletree

import java.util.*

class CalendarUtil {
    companion object{
        /**
         * 获取对应月份的日历表。
         * @param year 要获取的年份， 如：2022
         * @param month 要获取的月份， 如：三月份，传3
         * @return 返回的当月日历表
         */
        fun getDays(year: Int, month: Int): ArrayList<WeekDay>? {
            val weekDays = ArrayList<WeekDay>()
            val calendar = Calendar.getInstance()
            calendar[Calendar.YEAR] = year
            calendar[Calendar.MONTH] = month - 1 //月份从0开始，所以减1
            calendar[Calendar.DATE] = 1 //设置为1号
            val days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
            var weekDay = calendar[Calendar.DAY_OF_WEEK]
//            println(SimpleDateFormat("yyyy-MM-dd").format(calendar.time))
//            println("days:" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
//            println("weekDay:" + (calendar[Calendar.DAY_OF_WEEK] - 2)) //从周日开始且起始索引为1
            val chargeNum = Calendar.SATURDAY + 1
            for (i in 1..days) {
                weekDays.add(WeekDay(weekDay, i))
                ++weekDay
                if (weekDay == chargeNum) {
                    weekDay = Calendar.SUNDAY
                }
            }
            return weekDays
        }
    }

    class WeekDay {
        /**
         * 值为：
         * [java.util.Calendar.SUNDAY] 1
         * [java.util.Calendar.MONDAY] 2
         * [java.util.Calendar.TUESDAY] 3
         * [java.util.Calendar.WEDNESDAY] 4
         * [java.util.Calendar.THURSDAY] 5
         * [java.util.Calendar.FRIDAY] 6
         * [java.util.Calendar.SATURDAY] 7
         *
         * 从周一开始到周日时，前面可填充1号的week - MONDAY个值，遇SUNDAY时换行(注：当1号为周日值为1时直接添加6个空日期)；
         * 从周日开始到周六时，前面可填充1号的week - SUNDAY个值，遇SATURDAY时换行；
         */
        var week = 0
        var date = 0

        constructor(week: Int, date: Int) {
            this.week = week
            this.date = date
        }
    }
}